//
//  LYWelcomeController.swift
//  微博
//
//  Created by liuerying on 16/5/23.
//  Copyright © 2016年 liuerying. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

class LYWelcomeController: UIViewController {

    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        setupUI()
    }
    
    //视图将要出现时,开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //加载UI
   private func setupUI(){
    
    view.addSubview(iconView)
    view.addSubview(nameLabel)
    
    iconView.snp_makeConstraints { (make) in
        make.bottom.equalTo(self.view.snp_bottom).offset(-160)
        make.centerX.equalTo(self.view.snp_centerX)
        
        make.width.height.equalTo(90)
    }
    
    nameLabel.snp_makeConstraints { (make) in
        make.top.equalTo(iconView.snp_bottom).offset(16)
        make.centerX.equalTo(iconView.snp_centerX)
        
    }
    
        iconView.sd_setImageWithURL(LYUserAccountModel.sharedAccountViewModel.headURL)
    
    
        iconView.cornerRadius = 45
    
//    iconView.layer.masksToBounds
       nameLabel.alpha = 0
    
    }
    
    
    
    //动画效果
  private func startAnimation(){
    
    let offset = -ScreeHeight + 160
    
       UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { 
        
        self.iconView.snp_updateConstraints(closure: { (make) in
            make.bottom.equalTo(offset)
            
        })
        //提前刷新父视图
        self.view.layoutIfNeeded()

       }) { (_) in
            
            UIView.animateWithDuration(0.5, animations: { 
                self.nameLabel.alpha = 1
                }, completion: { (_) in
                    
              
                    //发送通知 切换根控制器
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
            })
            
    }
        
    }
    //隐藏电池栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    //懒加载子视图
    lazy var backView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    lazy var nameLabel : UILabel = UILabel(title:  LYUserAccountModel.sharedAccountViewModel.userAccount?.name, font:14.0, textcolor: UIColor.darkGrayColor(), alignment: .Center)
        


    

    
}
